package com.im.app.controller;


import com.im.app.authConfig.CurrentUser;
import com.im.app.authConfig.RequireLogin;
import com.im.app.model.Lixian;
import com.im.app.model.User;
import com.im.app.service.LixianService;
import com.im.app.utils.Response;
import com.im.app.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/lixian")
public class LixianController {

    @Resource
    private LixianService lixianService;

    @GetMapping(value = "/queryLixianList")
    @RequireLogin
    public ResponseResult<List<Lixian>> queryLixianList(@CurrentUser User user) {
        //调用业务层返回列表
        Lixian lixian = new Lixian();
        lixian.setUserId(user.getUserId());
        List<Lixian> lixianList = lixianService.selectLixianList(lixian);
        return Response.makeOKRsp(lixianList);
    }

    @GetMapping(value = "/getQuestion")
    public ResponseResult<List<Lixian>> getQuestion(String userId) {
        //调用业务层返回列表
        Lixian lixian = new Lixian();
        lixian.setUserId(userId);
        List<Lixian> lixianList = lixianService.selectLixianList(lixian);
        return Response.makeOKRsp(lixianList);
    }


    @GetMapping(value ="/find")
    @RequireLogin
    public ResponseResult<Lixian> findLixian(Integer id) {
        try {
            //调用业务层返回列表
            Lixian lixian = lixianService.selectLixianById(Long.valueOf(id));
            return Response.makeOKRsp(lixian);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.makeErrRsp("查询离线语信息异常");
        }
    }

    @GetMapping(value ="/delete")
    @RequireLogin
    public ResponseResult deleteLixian(Integer id) {
        int result = lixianService.deleteLixianById(Long.valueOf(id));
        if(result>0){
            return Response.makeOKRsp("删除成功");
        }else{
            return Response.makeErrRsp("删除失败");
        }

    }

    @PostMapping(value ="/add")
    @RequireLogin
    public ResponseResult<Object> addLixian(@CurrentUser User user, @RequestBody Lixian lixian) {
        lixian.setUserId(user.getUserId());
        int result = lixianService.insertLixian(lixian);
        if(result>0){
            return Response.makeOKRsp("添加成功");
        }else{
            return Response.makeErrRsp("添加失败");
        }
    }

    @PostMapping(value ="/update")
    @RequireLogin
    public ResponseResult updateLixian(@CurrentUser User user,@RequestBody Lixian lixian) {
        lixian.setUserId(user.getUserId());
        int result = lixianService.updateLixian(lixian);
        if(result>0){
            return Response.makeOKRsp("修改成功");
        }else{
            return Response.makeErrRsp("修改失败");
        }
    }








}
